package vg.modules.navigator.components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

import vg.services.data_base_manager.data.skeleton.GraphTreeElement;
import vg.services.data_base_manager.interfaces.IModel;
import vg.services.main_manager.MainManager;
import vg.services.user_interface_manager.interfaces.AUserInterface;

/**
 * This class realizes menu for comparison of two graph models.
 * @author tzolotuhin
 */
public class GraphTreeComparisonMenuItem implements IMenuItem {
	private final JMenuItem jItem;
	private final GraphModelComparisonAction action;
	/**
	 * Constructor.
	 * @param tree - tree of graph nodes.
	 * @param model - main storage.
	 * @param userInterface - main user interface.
	 */
	public GraphTreeComparisonMenuItem(final JTree tree, final IModel model, final AUserInterface userInterface) {		
		this.action = new GraphModelComparisonAction(tree, model, userInterface);
		this.jItem = new JMenuItem("[DISABLE]Comparison of two graph models");
		//final Icon icon = new ImageIcon("data/resources/textures/workflow.png");
		//this.jItem.setIcon(icon);
		this.jItem.addActionListener(this.action);
	}
	public JMenuItem getMenuItem() {
		return(this.jItem);
	}
	public boolean isVisible() {
		return(this.action.isVisible());
	}
	public void updateTheme() {
		SwingUtilities.updateComponentTreeUI(this.jItem);
	}
}
class GraphModelComparisonAction implements ActionListener {
	private final JTree tree;
	private final AUserInterface userInterface;
	/**
	 * Constructor.
	 * @param tree - tree of graph nodes.
	 * @param model - main storage.
	 */
	public GraphModelComparisonAction(final JTree tree, final IModel model, final AUserInterface userInterface) {
		this.tree = tree;
		this.userInterface = userInterface;
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(this.tree != null) {
			TreePath[] p = this.tree.getSelectionPaths();
			if(p != null && p.length == 2) {
				TreeModel tm = this.tree.getModel();
				if(tm == null) {
					MainManager.log.printDebug("[" + this.getClass().getName()+".actionPerformed] [BAD] Openning of comparison of two models(from navigator). Model = null.");
					return;
				} else {
					GraphTreeElement bufNode1 = ((GraphTreeElement)tm.getRoot()).vg_getNode(p[0]);
					GraphTreeElement bufNode2 = ((GraphTreeElement)tm.getRoot()).vg_getNode(p[1]);
					// delete TABs form nodes
					GraphTreeElement node1 = null, node2 = null;
					if(bufNode1 != null) {
						node1 = bufNode1.clone();
						node1.vg_remove(GraphTreeElement.DEF_TAB_TYPE);
					}
					if(bufNode2 != null) {
						node2 = bufNode2.clone();
						node2.vg_remove(GraphTreeElement.DEF_TAB_TYPE);
					}
					// add request to queue
					//IGraph gmc = MainManager.graphViewManager.newGraphTreeComparison(node1, node2);
					//UIRequestAddGraphTreeComparison request = new UIRequestAddGraphTreeComparison(null, gmc);
					//this.userInterface.addRequest(request);
				}
			}
		}
	}
	public boolean isVisible() {
		if(this.tree != null) {
			TreePath[] p = this.tree.getSelectionPaths();
			if(p != null && p.length == 2) {
				return true; 
			}
		}
		return false;
	}
}